package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.*;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//商品查询列表相关
@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    /**
     * 通过页面查询spu bo
     * 返回分页好的数据
     *
     * @param key      关键词-查询
     * @param saleable 可供出售--是否有库存
     * @param page     页面
     * @param rows     行
     * @return {@link ResponseEntity}<{@link PageResult}<{@link SpuBo}>>
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuBoByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows
    ) {

        PageResult<SpuBo> spuBoPageResult = this.goodsService.querySpuBoByPage(key, saleable, page, rows);
        if (CollectionUtils.isEmpty(spuBoPageResult.getItems())) {
            return ResponseEntity.notFound().build();//结果集没有内容
        }
        return ResponseEntity.ok(spuBoPageResult);
    }


    /**
     * 新增--扩展类完成接收
     * json数据格式的spu信息有spuDetail & sku
     * post请求
     *
     * @RequestBody注解来接收Json请求
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        this.goodsService.saveGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();//201
    }

    /**
     * 回显编辑按钮准备
     * spuid-查询spuDetail
     */
    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId) {
        SpuDetail spuDetail = this.goodsService.querySpuDetailBySpuId(spuId);
        if (spuDetail == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuDetail);
    }

    /**
     * 回显编辑按钮准备
     * spuid-sku信息
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkusBySpuId(@RequestParam("id") Long spuId) {
        List<Sku> skus = this.goodsService.querySkusBySpuId(spuId);
        if (CollectionUtils.isEmpty(skus)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }

    /**
     * 更新产品--实际上先删除在添加，多表之间的关系
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo) {
        this.goodsService.updateGoods(spuBo);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();//204
    }

    /**
     * 查询spu通过id
     *
     * @param id id
     * @return {@link ResponseEntity}<{@link Spu}>
     */
    @GetMapping("{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id) {
        Spu spu = goodsService.querySpuById(id);
        if (spu == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spu);
    }

    /**
     * 查询skuid
     *
     * @param skuId id
     * @return {@link ResponseEntity}<{@link Sku}>
     */
    /*根据Skuid查询参数信息  */
    @GetMapping("sku/{skuId}")
    public ResponseEntity<Sku> querySkuById(@PathVariable("skuId") Long skuId) {
        Sku sku = goodsService.querySkuById(skuId);
        if (sku == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(sku);
    }

    /**
     * 查询skuid
     *
     * @param skuId id
     * @return {@link ResponseEntity}<{@link Sku}>
     */
    /*根据Skuid查询库存信息  */
    @GetMapping("stock/{skuId}")
    public ResponseEntity<Integer> querySkuStockBySkuId(@PathVariable("skuId") Long skuId) {
        Integer stock = goodsService.querySkuStockBySkuId(skuId);
        return ResponseEntity.ok(stock);

    }

}
